# coding:utf-8

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette, QBrush
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QVBoxLayout, QHBoxLayout, QWidget, QLCDNumber
from PIL import ImageFilter, Image, ImageQt
from qtpy import QtGui

from ledclock import DigitalClock


class MyGaussianBlur(ImageFilter.Filter):
    name = "GaussianBlur"

    def __init__(self, radius=2):
        self.radius = radius

    def filter(self, image):
        return image.gaussian_blur(self.radius)


class MainWindow(QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.initUi()

    def initUi(self):
        self.resize(800, 400)
        self.setWindowTitle('亚克力测试')
        self.main_widget = QWidget()
        self.setCentralWidget(self.main_widget)
        layout = QVBoxLayout(self)
        layout.setAlignment(Qt.AlignCenter)
        led_timer = DigitalClock(self)
        led_timer.setMinimumSize(400, 100)
        layout.addWidget(led_timer)
        # self.lb.setStyleSheet('background-color: red;')
        self.setBackgroundImage()
        self.main_widget.setLayout(layout)

    def setBackgroundImage(self, p='./back.jpg'):
        img = Image.open('./back.jpg')
        img = img.filter(MyGaussianBlur(radius=40))  # type: Image.Image
        if img.mode == "RGB":
            img = img.convert("RGBA")
        pixmap = ImageQt.toqpixmap(img)
        pixmap = pixmap.scaled(self.size().width(), self.size().width(), aspectRatioMode=Qt.KeepAspectRatio)
        self.palette = QPalette()
        self.palette.setBrush(QPalette.Background, QBrush(pixmap))
        self.setPalette(self.palette)

    def resizeEvent(self, a0: QtGui.QResizeEvent) -> None:
        self.setBackgroundImage()



if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

